package com.tw.practice;

import java.lang.Integer;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 14-6-12.
 */
public class AsteriskPrinter {
    public void printOneAsterisk() {
        System.out.println("*");
    }

    public void printAsterisksOnOneLine(int n) {
        for (int i = 1; i <= n; i++) {
            System.out.print("*");
        }
        System.out.println();
    }

    public void printOneAsteriskOnEachLine(int n) {
        for (int i = 1; i <= n; i++) {
            System.out.println("*");
        }
    }

    public void printRightTriangle(int n) {
        for (int i = 1; i <= n; i++) {
            printAsterisksOnOneLine(i);
        }
    }

    public void printIsoscelesTriangle(int n) {
        for (int i = 1; i <= n; i++) {
            printBlank(n - i);
            printAsterisksOnOneLine(2 * i - 1);
        }
    }

    private void printBlank(int n) {
        for (int i = 0; i < n; i++) {
            System.out.print(" ");
        }
    }

    public void printDiamond(int n) {
        for (int i = 1; i <= 2 * n - 1; i++) {
            if (i > n) {
                printBlank(i - n);
                printAsterisksOnOneLine(4 * n - 1 - 2 * i);
            } else {
                printBlank(n - i);
                printAsterisksOnOneLine(2 * i - 1);
            }
        }
    }

    public void printDiamondWithName(String name, int n) {
        for (int i = 1; i <= 2 * n - 1; i++) {
            if (i > n) {
                printBlank(i - n);
                printAsterisksOnOneLine(4 * n - 1 - 2 * i);
            } else if (i == n) {
                System.out.println(name);
            } else {
                printBlank(n - i);
                printAsterisksOnOneLine(2 * i - 1);
            }
        }
    }

    public void FizzBuzz() {
        for (int i = 1; i <= 15; i++) {

            if (i % 3 == 0 && i % 5 == 0) {
                System.out.println("FizzBuzz");
            } else if (i % 3 == 0) {
                System.out.println("Fizz");
            } else if (i % 5 == 0) {
                System.out.println("Buzz");
            } else {
                System.out.println(i);
            }
        }
    }

    public List<Integer> generate(int n) {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 2; i < n; i++) {
            while (n != i) {
                if (n % i == 0) {
                    list.add(i);
                    n = n / i;
                } else
                    break;
            }
        }
        list.add(n);
        return list;
    }

    public static void main(String[] args) {
        AsteriskPrinter printer = new AsteriskPrinter();
        System.out.println("Easiest exercise ever: ");
        printer.printOneAsterisk();
        System.out.println("Draw a horizontal line: ");
        printer.printAsterisksOnOneLine(8);
        System.out.println("Draw a vertical line: ");
        printer.printOneAsteriskOnEachLine(3);
        System.out.println("Draw a right triangle: ");
        printer.printRightTriangle(3);
        System.out.println("Isosceles Triangle: ");
        printer.printIsoscelesTriangle(3);
        System.out.println("Diamond: ");
        printer.printDiamond(3);
        System.out.println("Diamond with Name: ");
        printer.printDiamondWithName("Bill", 3);
        System.out.println("FizzBuzz Exercise: ");
        printer.FizzBuzz();
        System.out.println("Prime Factors Exercise for 30: ");
        List list = printer.generate(30);
        System.out.println(list);
    }
}
